#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
快速查看本机局域网IP地址
用于Django服务器局域网访问配置
"""
import socket

def get_local_ip():
    """获取本机局域网IP地址"""
    try:
        # 创建一个UDP socket
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # 连接到一个远程地址（不会真正发送数据）
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
        s.close()
        return ip
    except Exception:
        return "127.0.0.1"

if __name__ == "__main__":
    local_ip = get_local_ip()
    print("=" * 50)
    print("Django 服务器局域网访问配置")
    print("=" * 50)
    print(f"\n你的局域网IP地址: {local_ip}")
    print(f"\n启动服务器命令:")
    print(f"  python manage.py runserver 0.0.0.0:8000")
    print(f"\n局域网访问地址:")
    print(f"  http://{local_ip}:8000")
    print("\n" + "=" * 50)
    print("注意事项:")
    print("1. 确保防火墙允许端口8000")
    print("2. 确保 ALLOWED_HOSTS = ['*'] 已设置")
    print("3. 确保在同一局域网内")
    print("=" * 50)


